package com.gome.axwl.aoc.web.controller.driver;

import com.anxl.sso.client.api.AccountInfo;
import com.anxl.sso.client.api.Dc;
import com.anxl.sso.client.api.Token;
import com.anxl.sso.client.support.AnxlSsoSupport;
import com.gome.axwl.aoc.common.constants.LoginConstant;
import com.gome.axwl.aoc.entity.dto.DriverIntegrationDto;
import com.gome.axwl.aoc.entity.dto.DriverIntegrationParamDto;
import com.gome.axwl.aoc.entity.dto.GradeRuleDto;
import com.gome.axwl.aoc.entity.dto.PageResultDto;
import com.gome.axwl.aoc.facade.DriverIntegrationFacade;
import com.gome.axwl.aoc.service.GradeRuleService;
import com.gome.axwl.aoc.service.sso.ServerGomeSsoClientService;
import com.gome.commons.core.ErrorCode;
import com.gome.commons.core.ResultData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 司机积分controller
 * @Auther: yinyuxin@gome.com.cn
 * @Date: 2018/4/27 17:13
 */
@Api(tags = "司机积分")
@RestController
@RequestMapping(value = "/driverIntegration")
public class DriverIntegrationController {

    @Autowired
    private GradeRuleService gradeRuleService;

    @Autowired
    private DriverIntegrationFacade driverIntegrationFacade;

    @Autowired
    private ServerGomeSsoClientService ssoClientService;

    /**
     * 查询等级值清单
     * @param date
     * @author yinyuxin
     * @return
     */
    @ApiOperation(value = "司机积分-获取等级值清单",  notes = "development by yinyuxin")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "date", value = "时间", paramType = "query",dataType="String",required = true)
    })
    @RequestMapping(value = "/queryGradeListOfMonth",method = RequestMethod.GET)
    public ResultData<List<GradeRuleDto>> queryGradeListOfMonth(String date){
        return gradeRuleService.queryGradeValuesByMonth(date);
    }


    /**
     * 按月查询所有司机的积分信息
     * @param driverIntegrationParamDto
     * @author yinyuxin
     * @return
     */
    @ApiOperation(value = "司机积分-按月查询所有司机的积分信息",  notes = "development by yinyuxin")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "integrationDate", value = "时间", paramType = "query",dataType="String",required = true),
            @ApiImplicitParam(name = "driverCode", value = "司机编码", paramType = "query",dataType="String",required = false),
            @ApiImplicitParam(name = "providerCode", value = "服务商编码", paramType = "query",dataType="String",required = false),
            @ApiImplicitParam(name = "dirverGradeId", value = "等级id", paramType = "query",dataType="String",required = false),
            @ApiImplicitParam(name = "integraTotalStart", value = "服务总分开始数", paramType = "query",dataType="int",required = false),
            @ApiImplicitParam(name = "integraTotalEnd", value = "服务总分截止数", paramType = "query",dataType="int",required = false),
            @ApiImplicitParam(name = "orderField", value = "排序字段 等级：level 服务总分：integra 好评率：favour 接单天数：workday 准时率：delivery", paramType = "query",dataType="String",required = false),
            @ApiImplicitParam(name = "orderSort", value = "排序方式 0：从大到小 1：从小到大", paramType = "query",dataType="int",required = false),
            @ApiImplicitParam(name = "pageSize", value = "每页记录数", paramType = "query",dataType="int",required = false),
            @ApiImplicitParam(name = "pageNum", value = "页码", paramType = "query",dataType="int",required = false)
    })
    @RequestMapping(value = "/queryAllDriverIntegration",method = RequestMethod.POST)
    public ResultData<PageResultDto<DriverIntegrationDto>> queryAllDriverIntegration(@RequestBody DriverIntegrationParamDto driverIntegrationParamDto, HttpServletRequest request, HttpServletResponse response){
        //获取账号得dc编码
        AccountInfo accountInfo = (AccountInfo)request.getSession().getAttribute(LoginConstant.LOGIN_USER);
        if(accountInfo == null) {
            Token token = (Token) request.getSession().getAttribute(AnxlSsoSupport.SSO_CONTEXT);
            accountInfo = ssoClientService.getUserByLocalUserId(token.getUid(), request, response);
        }
        List<Dc> dcList = accountInfo.getDcs();         //dc集合
        List<String> dcNos = new ArrayList<>();
        StringBuffer dcIds=new StringBuffer();
        if (dcList==null ||dcList.size()<=0){
            return new ResultData<>(ErrorCode.SUCCESS,ErrorCode.SUCCESS_MSG,new PageResultDto<>());
        }
        for (int i = 0; i < dcList.size(); i++) {
            dcIds.append(",").append(dcList.get(i).getDcId());
            dcNos.add(dcList.get(i).getDcNo());
        }
        String dcIdString;
        if (dcIds.length()>0){
            dcIdString=dcIds.toString().substring(1,dcIds.length());
        }else {
            dcIdString="0";
        }
        driverIntegrationParamDto.setDcNos(dcNos);
        driverIntegrationParamDto.setDcIds(dcIdString);

        return driverIntegrationFacade.queryAllDriverIntegration(driverIntegrationParamDto);
    }

    /**
     * 查询指定司机的历史积分明细
     * @param driverIntegrationParamDto
     * @author yinyuxin
     * @return
     */
    @ApiOperation(value = "司机积分-查询指定司机的历史积分明细",  notes = "development by yinyuxin")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "providerCode", value = "服务商编码", paramType = "query",dataType="String",required = true),
            @ApiImplicitParam(name = "driverCode", value = "司机编码", paramType = "query",dataType="String",required = true),
            @ApiImplicitParam(name = "orderField", value = " 排序字段 等级：level 服务总分：integra", paramType = "query",dataType="String",required = false),
            @ApiImplicitParam(name = "orderSort", value = "排序方式 0：从大到小 1：从小到大", paramType = "query",dataType="int",required = false),
            @ApiImplicitParam(name = "pageSize", value = "每页记录数", paramType = "query",dataType="int",required = false),
            @ApiImplicitParam(name = "pageNum", value = "页码", paramType = "query",dataType="int",required = false)
    })
    @RequestMapping(value = "/queryDriverIntegrationList",method = RequestMethod.POST)
    public ResultData<PageResultDto<DriverIntegrationDto>> queryDriverIntegrationList(@RequestBody  DriverIntegrationParamDto driverIntegrationParamDto){
        return driverIntegrationFacade.queryDriverIntegrationList(driverIntegrationParamDto.getProviderCode(),driverIntegrationParamDto.getDriverCode(),driverIntegrationParamDto.getOrderField(),driverIntegrationParamDto.getOrderSort()
                ,driverIntegrationParamDto.getPageNum(),driverIntegrationParamDto.getPageSize());
    }

    /**
     * 按月查询司机接单天数
     * @author yinyuxin
     * @return
     */
    @ApiOperation(value = "司机积分-查询指定司机的历史积分明细",  notes = "development by yinyuxin")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "driverId", value = "司机主键id", paramType = "query",dataType="int",required = true),
            @ApiImplicitParam(name = "month", value = "月份 yyyy-MM", paramType = "query",dataType="string",required = false)
    })
    @RequestMapping(value = "/queryDriverTakingsDetail",method = RequestMethod.GET)
    public ResultData queryDriverTakingsDetail(Integer driverId, String month){
        return driverIntegrationFacade.queryDriverTakingsDetail(driverId,month);
    }

}
